/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;


/**
 * https://securitit.blog.csdn.net/article/details/108046073
 *
 * 定义一个对象来帮助servlet向客户机发送响应.
 * Servlet容器创建一个ServletResponse对象，并将其作为参数传递给Servlet的service方法.
 * 要在MIME主体响应中：
 * 发送二进制数据，请使用getOutputStream返回的ServletOutputStream.
 * 发送字符数据，请使用getWriter返回的PrintWriter对象.
 * 要混合二进制和文本数据，例如，要创建多部分响应，请使用ServletOutputStream并手动管理字符部分.
 * MIME正文响应的字符集可以使用setCharacterEncoding和setContentType方法显式指定，也可以隐式使用setLocale方法指定,显式规范优先于隐式规范.
 * 如果没有指定字符集，将使用ISO-8859-1.
 * 必须在getWriter之前和提交要使用的字符编码的响应之前调用setCharacterEncoding、setContentType或setLocale方法.
 *
 *
 *
 * ServletResponse由Servlet容器来管理，当客户请求到来时，容器创建一个ServletRequest对象，封装请求数据，同时创建一个ServletResponse对象，
 * 封装响应数据。这两个对象将被容器作为service()方法的参数传递给Servlet，Servlet利用ServletRequest对象获取客户端发来的请求数据，利用ServletResponse对象发送响应数据。
 *
 * Defines an object to assist a servlet in sending a response to the client.
 * The servlet container creates a <code>ServletResponse</code> object and
 * passes it as an argument to the servlet's <code>service</code> method.
 *
 * <p>To send binary data in a MIME body response, use
 * the {@link ServletOutputStream} returned by {@link #getOutputStream}.
 * To send character data, use the <code>PrintWriter</code> object 
 * returned by {@link #getWriter}. To mix binary and text data,
 * for example, to create a multipart response, use a
 * <code>ServletOutputStream</code> and manage the character sections
 * manually.
 *
 * <p>The charset for the MIME body response can be specified
 * explicitly using the {@link #setCharacterEncoding} and
 * {@link #setContentType} methods, or implicitly
 * using the {@link #setLocale} method.
 * Explicit specifications take precedence over
 * implicit specifications. If no charset is specified, ISO-8859-1 will be
 * used. The <code>setCharacterEncoding</code>,
 * <code>setContentType</code>, or <code>setLocale</code> method must
 * be called before <code>getWriter</code> and before committing
 * the response for the character encoding to be used.
 * 
 * <p>See the Internet RFCs such as 
 * <a href="http://www.ietf.org/rfc/rfc2045.txt">
 * RFC 2045</a> for more information on MIME. Protocols such as SMTP
 * and HTTP define profiles of MIME, and those standards
 * are still evolving.
 *
 * @author 	Various
 *
 *
 * @see		ServletOutputStream
 *
 */
 
public interface ServletResponse {


    
    /**
     * 返回用于此响应中发送的正文的字符编码（MIME字符集）的名称.
     * 字符编码可能是使用setCharacterEncoding或setContentType方法显式指定的，也可能是隐式使用setLocale方法指定的.显式规范优先于隐式规范.
     * 在调用getWriter或在提交响应之后对这些方法的调用对字符编码没有影响.如果未指定字符编码，则返回ISO-8859-1.
     * Returns the name of the character encoding (MIME charset)
     * used for the body sent in this response.
     * The character encoding may have been specified explicitly
     * using the {@link #setCharacterEncoding} or
     * {@link #setContentType} methods, or implicitly using the
     * {@link #setLocale} method. Explicit specifications take
     * precedence over implicit specifications. Calls made
     * to these methods after <code>getWriter</code> has been
     * called or after the response has been committed have no
     * effect on the character encoding. If no character encoding
     * has been specified, <code>ISO-8859-1</code> is returned.
     * <p>See RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)
     * for more information about character encoding and MIME.
     *
     * @return		a <code>String</code> specifying the 例如：UTF-8.
     *			name of the character encoding, for
     *			example, <code>UTF-8</code>
     *
     */
  
    public String getCharacterEncoding();
    
    

    /**
     *
     * 返回用于此响应中发送的MIME正文的内容类型.
     * 在提交响应之前，必须使用setContentType指定正确的内容类型.
     * 如果未指定内容类型，则此方法返回null.
     * 如果已指定内容类型，并且已显式或隐式指定字符编码，如getCharacterEncoding或getWriter中所述，则返回的字符串中包含charset参数.如果未指定字符编码，则省略charset参数.
     * @return Content-Type，例如：text/html; charset=UTF-8
     *
     * Returns the content type used for the MIME body
     * sent in this response. The content type proper must
     * have been specified using {@link #setContentType}
     * before the response is committed. If no content type
     * has been specified, this method returns null.
     * If a content type has been specified and a
     * character encoding has been explicitly or implicitly
     * specified as described in {@link #getCharacterEncoding},
     * the charset parameter is included in the string returned.
     * If no character encoding has been specified, the
     * charset parameter is omitted.
     *
     * @return		a <code>String</code> specifying the
     *			content type, for example,
     *			<code>text/html; charset=UTF-8</code>,
     *			or null
     *
     * @since 2.4
     */
  
    public String getContentType();
    
    

    /**
     * 返回ServletOuputStream用于在response中写入二进制数据.
     * Servlet容器不编码二进制数据.
     * 调用ServletOutputStream的flush()方法提交response.
     * ServletOutputStream和PrintWriter不可同时使用.
     * @return ServletOutputStream实例.
     *
     * Returns a {@link ServletOutputStream} suitable for writing binary
     * data in the response. The servlet container does not encode the
     * binary data.  
     
     * <p> Calling flush() on the ServletOutputStream commits the response.
     
     * Either this method or {@link #getWriter} may 
     * be called to write the body, not both.
     *
     * @return				a {@link ServletOutputStream} for writing binary data	
     *
     * @exception IllegalStateException if the <code>getWriter</code> method
     * 					has been called on this response
     *
     * @exception IOException 		if an input or output exception occurred
     *
     * @see 				#getWriter
     *
     */

    public ServletOutputStream getOutputStream() throws IOException;
    
    

    /**
     * 返回一个getWriter对象，该对象可以向客户端发送字符文本.
     * PrintWriter使用getCharacterEncoding返回的字符编码.
     * 如果响应的字符编码没有按照getCharacterEncoding中的描述指定（即，该方法只返回默认值ISO-8859-1），
     *  则getWriter将其更新为ISO-8859-1.
     * 调用PrintWriter的flush()方法提交response.
     * PrintWriter和ServletOutputStream不可同时使用.
     *
     * Returns a <code>PrintWriter</code> object that
     * can send character text to the client.
     * The <code>PrintWriter</code> uses the character
     * encoding returned by {@link #getCharacterEncoding}.
     * If the response's character encoding has not been
     * specified as described in <code>getCharacterEncoding</code>
     * (i.e., the method just returns the default value 
     * <code>ISO-8859-1</code>), <code>getWriter</code>
     * updates it to <code>ISO-8859-1</code>.
     * <p>Calling flush() on the <code>PrintWriter</code>
     * commits the response.
     * <p>Either this method or {@link #getOutputStream} may be called
     * to write the body, not both.
     *
     * 
     * @return 		a <code>PrintWriter</code> object that 
     *			can return character data to the client 
     *
     * @exception UnsupportedEncodingException
     *			if the character encoding returned
     *			by <code>getCharacterEncoding</code> cannot be used
     *
     * @exception IllegalStateException
     *			if the <code>getOutputStream</code>
     * 			method has already been called for this 
     *			response object
     *
     * @exception IOException
     *			if an input or output exception occurred
     *
     * @see 		#getOutputStream
     * @see 		#setCharacterEncoding
     *
     */

    public PrintWriter getWriter() throws IOException;
    
    
    
    
    /**
     * 设置发送到客户端的响应的字符编码（MIME字符集），例如UTF-8.
     * 如果字符编码已经由setContentType或setLocale设置，则此方法将重写它.
     * 可以重复调用此方法来更改字符编码.如果在调用getWriter之后或在提交响应之后调用它，则此方法无效.
     * 如果协议提供了一种方法，容器必须将用于servlet响应的编写器的字符编码传递给客户机.
     * 在HTTP的情况下，字符编码作为文本媒体类型的Content-Type报头的一部分进行通信.
     * 请注意，如果Servlet没有指定内容类型，则字符编码不能通过HTTP头进行通信.但是，它仍然用于对通过Servlet响应的编写器写入的文本进行编码.
     *
     * Sets the character encoding (MIME charset) of the response
     * being sent to the client, for example, to UTF-8.
     * If the character encoding has already been set by
     * {@link #setContentType} or {@link #setLocale},
     * this method overrides it.
     * Calling {@link #setContentType} with the <code>String</code>
     * of <code>text/html</code> and calling
     * this method with the <code>String</code> of <code>UTF-8</code>
     * is equivalent with calling
     * <code>setContentType</code> with the <code>String</code> of
     * <code>text/html; charset=UTF-8</code>.
     * <p>This method can be called repeatedly to change the character
     * encoding.
     * This method has no effect if it is called after
     * <code>getWriter</code> has been
     * called or after the response has been committed.
     * <p>Containers must communicate the character encoding used for
     * the servlet response's writer to the client if the protocol
     * provides a way for doing so. In the case of HTTP, the character
     * encoding is communicated as part of the <code>Content-Type</code>
     * header for text media types. Note that the character encoding
     * cannot be communicated via HTTP headers if the servlet does not
     * specify a content type; however, it is still used to encode text
     * written via the servlet response's writer.
     *
     * @param charset 	a String specifying only the character set
     * 			defined by IANA Character Sets
     *			(http://www.iana.org/assignments/character-sets)
     *
     * @see		#setContentType
     * 			#setLocale
     *
     * @since 2.4
     *
     */

    public void setCharacterEncoding(String charset);
    
    


    /**
     * 在HTTP响应头Content-Length中设置响应体内容的长度.
     * Sets the length of the content body in the response
     * In HTTP servlets, this method sets the HTTP Content-Length header.
     *
     *
     * @param len 	an integer specifying the length of the 指定返回给客户端的内容的长度.设置Content-Length头.
     * 			content being returned to the client; sets
     *			the Content-Length header
     *
     */

    public void setContentLength(int len);
    
    

    /**
     * 如果尚未提交响应，则设置发送到客户端的响应的内容类型.
     * 给定的内容类型可以包括字符编码规范，例如，text/html;charset=UTF-8.
     * 如果在调用getWriter之前调用此方法，则仅从给定的内容类型设置响应的字符编码.
     * 可以重复调用此方法来更改内容类型和字符编码.如果在提交响应后调用此方法，则此方法无效.
     * 如果在调用getWriter之后或在提交响应之后调用它，则不会设置响应的字符编码.
     *
     * Sets the content type of the response being sent to
     * the client, if the response has not been committed yet.
     * The given content type may include a character encoding
     * specification, for example, <code>text/html;charset=UTF-8</code>.
     * The response's character encoding is only set from the given
     * content type if this method is called before <code>getWriter</code>
     * is called.
     * <p>This method may be called repeatedly to change content type and
     * character encoding.
     * This method has no effect if called after the response
     * has been committed. It does not set the response's character
     * encoding if it is called after <code>getWriter</code>
     * has been called or after the response has been committed.
     * <p>Containers must communicate the content type and the character
     * encoding used for the servlet response's writer to the client if
     * the protocol provides a way for doing so. In the case of HTTP,
     * the <code>Content-Type</code> header is used.
     *
     * @param type 	a <code>String</code> specifying the MIME 
     *			type of the content
     *
     * @see 		#setLocale
     * @see 		#setCharacterEncoding
     * @see 		#getOutputStream
     * @see 		#getWriter
     *
     */

    public void setContentType(String type);
    

    /**
     * 设置响应正文的首选缓冲区大小.Servlet容器将使用至少与请求大小相同的缓冲区.实际使用的缓冲区大小可以使用getBufferSize.
     * 更大的缓冲区允许在实际发送任何内容之前写入更多内容，从而为Servlet提供更多时间来设置适当的状态代码和标头.
     * 较小的缓冲区减少了服务器内存负载，并允许客户端更快地开始接收数据.
     * 必须在写入任何响应正文内容之前调用此方法.如果已写入内容或已提交response，则此方法将抛出IllegalStateException.
     *
     * Sets the preferred buffer size for the body of the response.
     * The servlet container will use a buffer at least as large as 
     * the size requested.  The actual buffer size used can be found
     * using <code>getBufferSize</code>.
     *
     * <p>A larger buffer allows more content to be written before anything is
     * actually sent, thus providing the servlet with more time to set
     * appropriate status codes and headers.  A smaller buffer decreases 
     * server memory load and allows the client to start receiving data more
     * quickly.
     *
     * <p>This method must be called before any response body content is
     * written; if content has been written or the response object has
     * been committed, this method throws an 
     * <code>IllegalStateException</code>.
     *
     * @param size 	the preferred buffer size
     *
     * @exception  IllegalStateException  	if this method is called after
     *						content has been written
     *
     * @see 		#getBufferSize
     * @see 		#flushBuffer
     * @see 		#isCommitted
     * @see 		#reset
     *
     */

    public void setBufferSize(int size);
    
    

    /**
     * 返回用于响应的实际缓冲区大小.
     * 如果不使用缓冲，则此方法返回0.
     * Returns the actual buffer size used for the response.  If no buffering
     * is used, this method returns 0.
     *
     * @return	 	the actual buffer size used
     *
     * @see 		#setBufferSize
     * @see 		#flushBuffer
     * @see 		#isCommitted
     * @see 		#reset
     *
     */

    public int getBufferSize();
    
    

    /**
     * 强制将缓冲区中的任何内容写入客户端.对此方法的调用会自动提交响应，这意味着将写入状态代码和标头.
     * Forces any content in the buffer to be written to the client.  A call
     * to this method automatically commits the response, meaning the status 
     * code and headers will be written.
     *
     * @see 		#setBufferSize
     * @see 		#getBufferSize
     * @see 		#isCommitted
     * @see 		#reset
     *
     */

    public void flushBuffer() throws IOException;
    
    
    
    /**
     * 清除响应中基础缓冲区的内容，而不清除标头或状态码.如果响应已提交，此方法将抛出IlleglaStateException.
     * Clears the content of the underlying buffer in the response without
     * clearing headers or status code. If the 
     * response has been committed, this method throws an 
     * <code>IllegalStateException</code>.
     *
     * @see 		#setBufferSize
     * @see 		#getBufferSize
     * @see 		#isCommitted
     * @see 		#reset
     *
     * @since 2.3
     */

    public void resetBuffer();
    

    /**
     * 返回一个布尔值，指示是否已提交响应.已提交的响应已写入其状态代码和标头.
     * Returns a boolean indicating if the response has been
     * committed.  A committed response has already had its status 
     * code and headers written.
     *
     * @return		a boolean indicating if the response has been
     *  		committed
     *
     * @see 		#setBufferSize
     * @see 		#getBufferSize
     * @see 		#flushBuffer
     * @see 		#reset
     *
     */

    public boolean isCommitted();
    
    

    /**
     * 清除缓冲区中存在的所有数据以及状态代码和标头.如果响应已提交，此方法将抛出IlleglaStateException.
     * Clears any data that exists in the buffer as well as the status code and
     * headers.  If the response has been committed, this method throws an 
     * <code>IllegalStateException</code>.
     *
     * @exception IllegalStateException  if the response has already been
     *                                   committed
     *
     * @see 		#setBufferSize
     * @see 		#getBufferSize
     * @see 		#flushBuffer
     * @see 		#isCommitted
     *
     */

    public void reset();
    
    

    /**
     * 在response未提交时，设置response的locale.
     * 如果尚未使用setContentType或setCharacterEncoding设置字符集、getWriter尚未被调用、response尚未被提交，它还会为区域设置适当的响应字符编码.
     * 可以重复调用此方法来更改区域设置和字符编码.如果在提交响应后调用该方法，则该方法无效.
     * 如果在使用字符集规范调用setContentType之后、调用setCharacterEncoding之后、调用了getWriter之后，或者在响应被提交之后调用，它不会设置响应的字符编码.
     * 如果协议提供了一种方法，容器必须将用于servlet响应的编写器的区域设置和字符编码传递给客户端.
     * 在HTTP的情况下，语言环境通过Content-Language报头进行通信，字符编码作为文本媒体类型的Content-Type报头的一部分.
     * 请注意，如果Servlet没有指定内容类型，则字符编码不能通过HTTP头进行通信.但是，它仍然用于对通过Servlet响应的编写器写入的文本进行编码.
     *
     * Sets the locale of the response, if the response has not been
     * committed yet. It also sets the response's character encoding
     * appropriately for the locale, if the character encoding has not
     * been explicitly set using {@link #setContentType} or
     * {@link #setCharacterEncoding}, <code>getWriter</code> hasn't
     * been called yet, and the response hasn't been committed yet.
     * If the deployment descriptor contains a 
     * <code>locale-encoding-mapping-list</code> element, and that
     * element provides a mapping for the given locale, that mapping
     * is used. Otherwise, the mapping from locale to character
     * encoding is container dependent.
     * <p>This method may be called repeatedly to change locale and
     * character encoding. The method has no effect if called after the
     * response has been committed. It does not set the response's
     * character encoding if it is called after {@link #setContentType}
     * has been called with a charset specification, after
     * {@link #setCharacterEncoding} has been called, after
     * <code>getWriter</code> has been called, or after the response
     * has been committed.
     * <p>Containers must communicate the locale and the character encoding
     * used for the servlet response's writer to the client if the protocol
     * provides a way for doing so. In the case of HTTP, the locale is
     * communicated via the <code>Content-Language</code> header,
     * the character encoding as part of the <code>Content-Type</code>
     * header for text media types. Note that the character encoding
     * cannot be communicated via HTTP headers if the servlet does not
     * specify a content type; however, it is still used to encode text
     * written via the servlet response's writer.
     * 
     * @param loc  the locale of the response
     *
     * @see 		#getLocale
     * @see 		#setContentType
     * @see 		#setCharacterEncoding
     *
     */

    public void setLocale(Locale loc);
    
    

    /**
     * 返回使用setLocale指定的区域设置.setLocale在response提交之后不会生效.
     *      如果没有指定locale，返回容器默认的locale.
     *
     * Returns the locale specified for this response
     * using the {@link #setLocale} method. Calls made to
     * <code>setLocale</code> after the response is committed
     * have no effect. If no locale has been specified,
     * the container's default locale is returned.
     * 
     * @see 		#setLocale
     *
     */

    public Locale getLocale();



}





